package com.qimingyu.array.binarysearch;

/**
 * 704. 二分查找
 * 简单
 * 1.4K
 * 相关企业
 * 给定一个 n 个元素有序的（升序）整型数组 nums 和一个目标值 target  ，
 * 写一个函数搜索 nums 中的 target，如果目标值存在返回下标，否则返回 -1。
 * <p>
 * 示例 1:
 * <p>
 * 输入: nums = [-1,0,3,5,9,12], target = 9
 * 输出: 4
 * 解释: 9 出现在 nums 中并且下标为 4
 * 示例 2:
 * <p>
 * 输入: nums = [-1,0,3,5,9,12], target = 2
 * 输出: -1
 * 解释: 2 不存在 nums 中因此返回 -1
 */

public class BinarySearch {
    /**
     * 基础版二分查找
     *
     * @param nums
     * @param target
     * @return
     */
    public static int binarySearchBasic(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            //使用位移运算防止数字的溢出  向右位移一位相当于除以二
            int middle = (left + right) >>> 1;
            int middleNum = nums[middle];
            if (target < middleNum) {
                right = middle - 1;
            } else if (middleNum < target) {
                left = middle + 1;
            } else {
                return middle;
            }
        }
        return -1;
    }

    /**
     * 改进后的二分查找，用right作为右边界，不参与直接的比较
     *
     * @param nums
     * @param target
     * @return
     */
    public static int binarySearchImprove(int[] nums, int target) {
        int left = 0;
        int right = nums.length;
        while (left < right) {
            int middle = (left + right) >>> 1;
            int middleNum = nums[middle];
            if (target < middleNum) {
                right = middle;
            } else if (middleNum < target) {
                left = middle + 1;
            } else {
                return middle;
            }
        }
        return -1;
    }

    /**
     * 二分查找在有重复元素的情况下，返回重复元素中的最左边的元素
     *
     * @param nums
     * @param target
     * @return
     */
    public static int binarySearchLeftMost(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int candidate = -1;
        while (left <= right) {
            int middle = (left + right) >>> 1;
            int middleNum = nums[middle];
            if (target < middleNum) {
                right = middle - 1;
            } else if (middleNum < target) {
                left = middle + 1;
            } else {
                candidate = middle;
                right = middle - 1;
            }
        }
        return candidate;
    }

    /**
     * 二分查找在有重复元素的情况下，返回重复元素中的最右边的元素
     *
     * @param nums
     * @param target
     * @return
     */
    public static int binarySearchRightMost(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int candidate = -1;
        while (left <= right) {
            int middle = (left + right) >>> 1;
            int middleNum = nums[middle];
            if (target < middleNum) {
                right = middle - 1;
            } else if (middleNum < target) {
                left = middle + 1;
            } else {
                candidate = middle;
                left = middle + 1;
            }
        }
        return candidate;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 2, 2, 2, 5, 9, 10, 11};
        System.out.println(binarySearchBasic(arr, 11));

        System.out.println("bi narySearchLeftMost :" + binarySearchLeftMost(arr, 2));
        System.out.println("binarySearchRightMost :" + binarySearchRightMost(arr, 2));


    }
}
